package Phase;

import Entities.*;
import Entities.Advantages.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

class FilePhaseLoader implements PhaseLoader {

    private String phase;
    private Scene scene;
    ArrayList<Integer> numeroList;
    ArrayList<Integer> brickProperties;

    public FilePhaseLoader(String phase) {
        this.phase = phase;
    }

    @Override
    public Scene load() {
        scene = new Scene();

        Boundary boundary = new Boundary(0, 0, 399, 299);
        scene.addChild(boundary);

        try {
            tratarPrueba();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(FilePhaseLoader.class.getName()).log(Level.SEVERE, null, ex);
        }

        return scene;
    }

    public void tratarPrueba() throws FileNotFoundException {
        try {
            Random r = new Random(System.currentTimeMillis());
            Integer caracter = 0;
            int numero = 0;
            int brickNumber = 0;
            numeroList = new ArrayList();
            brickProperties = new ArrayList();

            try (FileReader input = new FileReader(phase)) {

                while (caracter != -1) {

                    caracter = input.read();
                    numero = Character.getNumericValue(caracter);

                    if (numero != -1) {
                        numeroList.add(numero);
                    } else {
                        while (numeroList.size() > 0) {
                            brickNumber = (int) (brickNumber + (numeroList.get(0) * Math.pow((double) 10, (double) numeroList.size() - 1)));
                            numeroList.remove(0);
                        }

                        if (caracter != 10) {
                            brickProperties.add(brickNumber);
                            brickNumber = 0;
                        }
                    }

                    Brick brick = new Brick();
                    if (brickProperties.size() == 3) {
                        brick.setX(brickProperties.get(0));
                        brick.setY(brickProperties.get(1));
                        brick.setResistance(brickProperties.get(2));

                        while (brickProperties.size() > 0) {
                            brickProperties.remove(0);
                        }
                        scene.addChild(brick);
                    }
                    switch (r.nextInt(3)) {
                        case 1: {
                            brick.setAdvantage(new DecreaseBarSize(brick.getX(), brick.getY(), 20, 20));
                            break;
                        }
                        case 2: {
                            brick.setAdvantage(new DecreaseBallSpeed(brick.getX(), brick.getY(), 20, 20));
                            break;
                        }
                        case 3: {
                            brick.setAdvantage(new IncreaseBarSize(brick.getX(), brick.getY(), 20, 20));
                            break;
                        }
                        default: {
                            brick.setAdvantage(new IncreaseBallSpeed(brick.getX(), brick.getY(), 20, 20));
                            break;
                        }
                    }
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(FilePhaseLoader.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
